package com.myshiro.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

//@Configuration
public class ShiroConfig {

    //开启 方法级别的 shiro的授权的注解
    //需要用到2个spring的Bean
    //@DefaultAdvisorAutoProxyCreator
    //
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor advisor =new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

    //自定义的Realm
    @Bean
    public MyShiroRealm myShiroRealm(){
        return new MyShiroRealm();
    }
    @Bean
    public SecurityManager securityManager(){
        DefaultWebSecurityManager securityManager =new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        return securityManager;
    }
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);


        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/main");
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorizedUrl");


        Map<String, String> filterMapChain =new LinkedHashMap<>();
        filterMapChain.put("/login","anon");
        filterMapChain.put("/doLogin.html","anon");
        filterMapChain.put("/logout","logout");//注销shiro中的主体信息

        filterMapChain.put("/main","authc");


        //动态授权
        //查全部权限
        // filterMapChain.put("请求url","perms[具体的权限]");  -->去mysql里面查

        //添加静态授权,  权限A ->可访问A url
        filterMapChain.put("/user/list","perms[user:list]");//登录用户有
        filterMapChain.put("/user/add","perms[user:add]"); //管理员有
        //filterMapChain.put("/user/update","perms[user:update]"); //管理员有
        filterMapChain.put("/user/view/**","perms[L0501]"); //非管理员



        filterMapChain.put("/**","authc");//所有请求,都要经过shiro 的授权
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMapChain);

        return shiroFilterFactoryBean;
    }

    @Bean
    public ShiroDialect shiroDialect(){
        return  new ShiroDialect(); //注入shiro的方言  --> 在thymeleaf中可以使用 shiro的标签
    }
}
